﻿<%@ Page Title="" Language="C#" MasterPageFile="~/temp/main/main.Master" AutoEventWireup="true" CodeBehind="cart.aspx.cs" Inherits="Donal.Xms.Web.temp.main.cart" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <script>        
        $(function () {
            function SetListPrice() {
                $("input[id^=number_]").each(function () {
                    let id = $(this).attr("id").replace("number_", "");
                    let quantity = parseInt($(this).val());
                    let price = parseFloat($("#price_" + id).html());
                    $("#amount_" + id).html((quantity * price).toFixed(2));
                })
            }

            SetListPrice();

            $("input[id^=number_]").change(function () {
                SetListPrice();
                let i = $(this).attr("id").replace("number_", "");
                let g = $("#hid_" + i).val();
                let q = $(this).val();
                if (!$("#cb_" + i).prop('checked')) { $("#cb_" + i).prop('checked', true) }
                $.post("/tools/submit_ajax.ashx?action=set_cart_quantity_" + new Date().getTime(), { i, g, q }, () => { }, 'json')
                SetAmounts();
            });

            function SetAmounts() {
                let count = 0;
                let amount = 0;

                $("input[id^=cb_]").each(function () {
                    let id = $(this).attr("id").replace("cb_", "");
                    if ($(this).prop('checked')) {
                        count++;
                        amount += parseFloat($("#amount_" + id).html());
                    }
                });

                $("#lbCount").html(count);
                $("#lbAmount").html(amount);
            }

            $("input[id^=cb_]").change(function () {
                SetAmounts();
            });

            $("[id^=del_]").click(function () {
                let i = $(this).attr("id").replace("del_", "");
                let g = $("#hid_" + i).val();
                let q = $(this).val();
                dialog({
                    title: '提示',
                    content: '您确定要删除吗？',
                    okValue: '确定',
                    ok: function () {
                        $.post("/tools/submit_ajax.ashx?action=del_cart_goods_" + new Date().getTime(), { i, g, q }, ref => {
                            if (ref.status == 1) {
                                $("#tr_" + i).remove(); SetAmounts();
                            } else {
                                dialog({
                                    title: "提示",
                                    content: ref.msg,
                                    okValue: '确定',
                                    ok: function () { }
                                }).showModal();
                            }
                        }, 'json')
                    },
                    cancelValue: '取消',
                    cancel: function () { }
                }).showModal();
            });

            $("#btnAll").click(function () {
                if ($("#btnAll").val() == "全选") {
                    $("input[id^=cb_]").each(function () {
                        $(this).prop('checked', true);
                    });
                    $("#btnAll").val("取消");
                } else {
                    $("input[id^=cb_]").each(function () {
                        $(this).prop('checked', false);
                    });
                    $("#btnAll").val("全选");
                }

                SetAmounts();
            })

            $("#btnDel").click(function () {

                if ($("input[id^=cb_]:checked").length == 0) {
                    dialog({
                        title: "提示",
                        content: "请选择要删除的记录！",
                        okValue: '确定',
                        ok: function () { }
                    }).showModal();
                    return;
                }

                let i = 0;
                let g = 0;
                let q = 0;
                dialog({
                    title: '提示',
                    content: '您确定要删除所选的记录吗？',
                    okValue: '确定',
                    ok: function () {
                        let promises = []; // 存储所有的Promise
                        $("input[id^=cb_]").each(function () {
                            let checkbox = $(this);
                            if (checkbox.prop('checked')) {
                                i = checkbox.attr("id").replace("cb_", "");
                                g = $("#hid_" + i).val();
                                q = $("#number_" + i).val();
                                // 创建一个Promise
                                let promise = new Promise((resolve, reject) => {
                                    $.post("/tools/submit_ajax.ashx?action=del_cart_goods_" + new Date().getTime(), { i, g, q }, function (ref) {
                                        if (ref.status == 1) {
                                            resolve();
                                        } else {
                                            reject(ref.msg);
                                        }
                                    }, 'json');
                                });
                                promises.push(promise); // 将Promise添加到数组中
                            }
                        });

                        // 使用Promise.all等待所有的Promise完成
                        Promise.all(promises).then(() => {
                            // 所有删除成功后，删除对应的行
                            $("input[id^=cb_]:checked").each(function () {
                                let i = $(this).attr("id").replace("cb_", "");
                                $("#tr_" + i).remove();
                            });

                            SetAmounts();

                        }).catch((error) => {
                            // 处理错误
                            dialog({
                                title: "提示",
                                content: error,
                                okValue: '确定',
                                ok: function () { }
                            }).showModal();
                        });
                    },
                    cancelValue: '取消',
                    cancel: function () { }
                }).showModal();
            })

            $("#btnOrder").click(function () {
                if ($("input[id^=cb_]:checked").length == 0) {
                    dialog({
                        title: "提示",
                        content: "请选择要购买的记录！",
                        okValue: '确定',
                        ok: function () { }
                    }).showModal();
                    return;
                }
                let i = "";
                let gs = "";
                $("input[id^=cb_]").each(function () {
                    let checkbox = $(this);
                    if (checkbox.prop('checked')) {
                        i = checkbox.attr("id").replace("cb_", "");
                        gs += $("#hid_" + i).val() + "-" + $("#number_" + i).val() + ",";
                    }
                });

                $.post("/tools/submit_ajax.ashx?action=confirm_" + new Date().getTime(), { gs }, function (ref) {
                    if (ref.status == 1) {
                        window.location.href = "/confirm.html";
                    } else {
                        reject(ref.msg);
                    }
                }, 'json');

            });

        })
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    <div>
        <asp:Repeater ID="rptCategory" runat="server" OnItemDataBound="rptCategory_ItemDataBound">
            <HeaderTemplate>
                <div>
                    <table style="width: 100%">
                        <tr>
                            <th style="width: 5%">选择</th>
                            <th style="width: 20%">规格名称</th>
                            <th style="width: 10%">产品名称</th>
                            <th style="width: 10%">产品类别</th>
                            <th style="width: 10%">含税单价</th>
                            <th style="width: 10%">订购数量</th>
                            <th style="width: 10%">小计</th>
                            <th style="width: 5%">操作</th>
                        </tr>
            </HeaderTemplate>
            <ItemTemplate>

                <asp:HiddenField ID="hidId" runat="server" Value='<%#Eval("id") %>' />

                <asp:Repeater ID="rptList" runat="server">
                    <ItemTemplate>
                        <tr id="tr_<%# Eval("id") %>">
                            <td style="text-align: center;">
                                <input type="checkbox" id="cb_<%# Eval("id") %>" />
                                <input type="hidden" id="hid_<%# Eval("id") %>" value="<%# Eval("goods_id") %>" />
                            </td>
                            <td style="text-align: center;">
                                <%# Eval("title") %>
                                <p>
                                    <%# GetFields(int.Parse(Eval("goods_id").ToString())) %>
                                </p>
                            </td>
                            <td style="text-align: center;"><%# Eval("product_title") %></td>
                            <td style="text-align: center;"><%# Eval("category_title") %></td>
                            <td style="text-align: center;">￥<label id="price_<%# Eval("id") %>"><%# Eval("sell_price").ToString() %></label>
                            </td>
                            <td style="text-align: center;">
                                <input type="number" value="<%# Eval("quantity") %>" id="number_<%# Eval("id") %>" max="999" min="1" />
                            </td>
                            <td style="text-align: center;">￥<label id="amount_<%# Eval("id") %>">0</label></td>
                            <td style="text-align: center;">
                                <a id="del_<%# Eval("id") %>">删除</a>
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
                <tr>
                    <td colspan="8" style="border-bottom: 1px solid #808080"></td>
                </tr>

            </ItemTemplate>
            <FooterTemplate>
                </table>
                </div>
            </FooterTemplate>
        </asp:Repeater>



    </div>
    <div>
        已选择：<label id="lbCount">0</label>&nbsp;&nbsp;￥<label id="lbAmount">0.00</label>
        <input type="button" id="btnAll" value="全选" />&nbsp;&nbsp;<input type="button" id="btnDel" value="删除" />&nbsp;&nbsp;<input type="button" id="btnOrder" value="购买" />
    </div>
</asp:Content>
